﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.CV.UI;
using TagPropagator;
using System.IO;
namespace Histogram
{
    class Program
    {

        public static void SaveHist(DenseHistogram histogram0, Size size, String file, Color c)
        {
            Image<Bgr, byte> imgRed = new Image<Bgr, byte>(size);
            float min, max;
            int[] minloc, maxloc;
            histogram0.MinMax(out min, out max, out minloc, out maxloc);
            Image<Bgr, byte> h0Image = new Image<Bgr, byte>(new Size(256, 64));
            h0Image.SetZero();
            int scaleX = 20;
            int scaleY = 1;
            for (int i = 0; i < 19; i++)
            {
                double histValue = histogram0[i];
                double nextValue = histogram0[i + 1];

                Point p1 = new Point(i * scaleX, 64 * scaleY);
                Point p2 = new Point(i * scaleX + scaleX, 64 * scaleY);
                Point p3 = new Point(i * scaleX + scaleX, (int)((64 - nextValue * 64 / max) * scaleY));
                Point p4 = new Point(i * scaleX, (int)((64 - histValue * 64 / max) * scaleY));
                int numPts = 5;
                Point[] pts = { p1, p2, p3, p4, p1 };

                h0Image.FillConvexPoly(pts, new Bgr(c));
            }
            h0Image.Save(file);
        }
        static void Main(string[] args)
        {

            //String r = @"C:\Users\bartek\Pictures\Focie na wzaip\mirflickr08_images1\images\im121.jpg";
            //Node g = new Node();
            //FeatureExtractor ff = new FeatureExtractor();
            //g.Image = r;
            //g.Features = ff.ExtractFeatures(g.ImageFile);
            //Comparer co = new Comparer();
            //String bla = @"C:\Users\bartek\Pictures\Focie na wzaip\mirflickr08_images1\images";
            //foreach(var file in Directory.GetFiles(bla)) 
            //{
            //    Node h = new Node();
            //    h.Image = file;
            //    h.Features = ff.ExtractFeatures(h.ImageFile);
            //    double com = co.Compare(g, h);
            //    Console.WriteLine(file + " " + com);
            //}
            String s1 = "C:\\1.jpg";
            String s2 = "C:\\2.jpg";
            var bm1 = new Bitmap(s1);
            var bm2 = new Bitmap(s2);
            Node n1 = new Node();
            n1.Image = s1;
            Node n2 = new Node();
            n2.Image = s2;

            FeatureExtractor f = new FeatureExtractor();
            n1.Features = f.ExtractFeatures(n1.ImageFile);

            n2.Features = f.ExtractFeatures(n2.ImageFile);

            Image<Bgr, byte> img = new Image<Bgr, byte>(bm1);
            Image<Gray, Byte>[] channels = img.Split();
            DenseHistogram histogram0 = new DenseHistogram(20, new RangeF(0, 255));
            CvInvoke.cvCalcHist(new System.IntPtr[] { channels[0].Ptr }, histogram0, false, IntPtr.Zero);
            SaveHist(histogram0, img.Size, "r1.jpg", Color.Red);
            DenseHistogram histogram1 = new DenseHistogram(20, new RangeF(0, 255));
            CvInvoke.cvCalcHist(new System.IntPtr[] { channels[1].Ptr }, histogram1, false, IntPtr.Zero);
            SaveHist(histogram1, img.Size, "g1.jpg", Color.Green);
            DenseHistogram histogram2 = new DenseHistogram(20, new RangeF(0, 255));
            CvInvoke.cvCalcHist(new System.IntPtr[] { channels[2].Ptr }, histogram2, false, IntPtr.Zero);
            SaveHist(histogram2, img.Size, "b1.jpg", Color.Blue);

            Image<Bgr, byte> img2 = new Image<Bgr, byte>(bm2);
            Image<Gray, Byte>[] channels2 = img2.Split();
            DenseHistogram histogram02 = new DenseHistogram(20, new RangeF(0, 255));
            CvInvoke.cvCalcHist(new System.IntPtr[] { channels2[0].Ptr }, histogram02, false, IntPtr.Zero);
            SaveHist(histogram02, img2.Size, "r2.jpg", Color.Red);
            DenseHistogram histogram12 = new DenseHistogram(20, new RangeF(0, 255));
            CvInvoke.cvCalcHist(new System.IntPtr[] { channels2[1].Ptr }, histogram12, false, IntPtr.Zero);
            SaveHist(histogram12, img2.Size, "g2.jpg", Color.Green);
            DenseHistogram histogram22 = new DenseHistogram(20, new RangeF(0, 255));
            CvInvoke.cvCalcHist(new System.IntPtr[] { channels2[2].Ptr }, histogram22, false, IntPtr.Zero);
            SaveHist(histogram22, img2.Size, "b2.jpg", Color.Blue);

            Comparer c = new Comparer();
            Console.WriteLine(c.Compare(n1, n2));
            Console.ReadKey();
        }
    }
}
